
PARAM ( [switch]$Clean )



# Ideally use the Storage Module, however this only supports WS2012 and up... Not WS2008
function RunDiskpartScript ( $Script ) {
    Write-Output "`n******************************"
    Write-Output $Script
    Write-Output "******************************"
    $Script | Diskpart.exe
    Start-Sleep -Seconds 2
}


function OnlineAllDrives {
    # Beware, sometimes not a space but a TAB character between 'Fixed' and 'hard'
    $DiskList = Get-WmiObject -Class Win32_DiskDrive | Where-Object { $_.MediaType -match 'Fixed\shard disk media' }
    ForEach ($Disk in $DiskList) {
        $DPScript = @"
Select disk $($Disk.Index)
online disk noerr
attributes disk clear readonly noerr
"@
        RunDiskPartScript $DPScript
    }
}

function IsFreeDrive ($Letter) {
    # Concatenate :  if only C is given.
    if ($Letter.Length() -eq 1) { $Letter += ":" }
    # $LogicalDisks moet al gevuld zijn...
    if ($LogicalDisks | Where-Object { $_.DeviceID -eq $Letter } ) {
        Write-Output $False
    } else {
        Write-Output $True
    }
}

function RenameAllDrives {

    $LogicalDisks = Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID,@{n='NewDeviceID';e={if (Test-Path "$($_.DeviceID)\Diskcfg.sys") { (Get-IniContent -FilePath "$($_.DeviceID)\Diskcfg.sys")["Configuration"]["Drive"]  } else {$_.DeviceID }}} ,DriveType,ProviderName,VolumeName,FreeSpace,Size
#   $LogicalDisks is now:
#    DeviceID     : E:
#    NewDeviceID  : D:
#    DriveType    : 3
#    ProviderName :
#    VolumeName   : Programs
#    FreeSpace    : 423563497472
#    Size         : 500105736192

    # Now we search for the first available drive letter, in between "C:" and "Z:"
    $j=66 ' B
    While (!(IsFreeDrive) ) {
        j = j + 1
        strFreeDriveLetter = Chr(j)
    Loop Until IsFreeDrive(strFreeDriveLetter) Or j>=90

}

function RenameDrive ($OldDriveLtr, $NewDriveLtr) {
# This sub renames a drive to another driveletter.
#  also the array with current driveletter mappings need to be updated, and the freedriveletter is also changed.
    $DPScript = @"
Select Volume $OldDriveLtr
Assign Letter $NewDriveLtr
"@
    RunDiskPartScript $DPScript
    $FreeDriveLetter=$OldDriveLtr

}

function CreateConfigFile ($DriveLetter, $Size, $Label, $FS, $Clustersize) {
    $DriveLetter = $DriveLetter.Replace(':','').Replace('\','').Trim()
    "Do NOT delete this file or change it's contents!" | Out-File "$($DriveLetter):\Diskcfg.sys"
    "[Configuration]"          | Out-File "$($DriveLetter):\Diskcfg.sys" -Append
    "Drive=$DriveLetter"       | Out-File "$($DriveLetter):\Diskcfg.sys" -Append
    "Size=$Size"               | Out-File "$($DriveLetter):\Diskcfg.sys" -Append
    "Label=$Label"             | Out-File "$($DriveLetter):\Diskcfg.sys" -Append
    "FS=$FS"                   | Out-File "$($DriveLetter):\Diskcfg.sys" -Append
    "Clustersize=$ClusterSize" | Out-File "$($DriveLetter):\Diskcfg.sys" -Append
}


Import-Module C:\SVN\DeployLib\trunk\Deploy\PowerShell\Modules\DeployLib
$VerbosePreference = "Continue"
# Get-DeployData Windows Common NT4Domain

$FreeDriveLetter=$nul
OnlineAllDrives
RenameAllDrives

# Retrieve the disk index for our boot disk. Leave that one intact.
$OSDiskNr = (Get-WmiObject Win32_DiskPartition | ? { $_.BootPartition } ).DiskIndex
$Disks = Get-WmiObject Win32_DiskDrive -Filter "Index <> $OsDiskNr"


ForEach ($Disk in $Disks ) {
  if (($Disk.Partitions -eq 0) -or $Clean) {

  switch ($Disk.Index) {
    1 { $Letter="D" ; $Label="Programs" }
    2 { $Letter="P" ; $Label="PageFile" }
    default { $Letter = $Nul }
  }

  if ($Letter) {
    if ( Get-WmiObject Win32_Volume -Filter "caption = '$Letter:\\'" ) {
        Write-Output "Volume $Letter already exists... Skipping !"
    } else {

        Write-Output "Disk $($Disk.Index) Letter $Letter Label $Label"

        if ($Clean) {
          Write-Output "Cleaning disk $($Disk.Index) ... "
          $DPScript = @"
Select disk $($Disk.Index)
Clean
"@
          RunDiskPartScript $DPScript
        }

        $DPScript = @"
Select disk $($Disk.Index)
online disk noerr
attributes disk clear readonly noerr
create partition primary noerr
"@
          RunDiskPartScript $DPScript
 
          $DPScript = @"
Select disk $($Disk.Index)
Select Partition 1
Format FS=NTFS Label=$Label quick noerr
assign letter $Letter
"@
          RunDiskPartScript $DPScript

        }
    }

  } else {
    Write-Output "Disk $($Disk.Index) already has $($Disk.Partitions) partitions"
  }
}

